package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {
        ArrayList<JSONObject> list = new ArrayList<>();
        //查询所有分类数据
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

        //按照一级分类Id进行分组 key:一级分类id value:一级分类id对应的集合数据
        Map<Long, List<BaseCategoryView>> listMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));


        Set<Map.Entry<Long, List<BaseCategoryView>>> entries = listMap.entrySet();
        Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = entries.iterator();

        while (iterator.hasNext()) {
            Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
            Long category1Id = entry.getKey();
//			System.out.println("key = " + category1Id);
            List<BaseCategoryView> categoryViewList = entry.getValue();
//			System.out.println("value = " + categoryViewList);
            String category1Name = categoryViewList.get(0).getCategory1Name();
//			System.out.println("category1Name = " + category1Name);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("categoryId", category1Id);
            jsonObject.put("categoryName", category1Name);

            //获取二级数据
            Map<Long, List<BaseCategoryView>> category2ListMap = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category2ListMap.entrySet().iterator();
            ArrayList<JSONObject> chile2 = new ArrayList<>();
            while (iterator1.hasNext()) {

                Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
                Long category2Id = entry1.getKey();
                List<BaseCategoryView> categoryViewList1 = entry1.getValue();
//				System.out.println("value" + value);
                String category2Name = entry1.getValue().get(0).getCategory2Name();
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", category2Id);
                jsonObject2.put("categoryName", category2Name);
                chile2.add(jsonObject2);
                jsonObject.put("categoryChild", chile2);
                /*
                 * 获取三级数据
                 */
                List<JSONObject> categoryViewList2 = categoryViewList1.stream().map(item -> {
                    JSONObject jsonObject3 = new JSONObject();
                    item.getCategory3Id();
                    item.getCategory3Name();
                    jsonObject3.put("categoryId", item.getCategory3Id());
                    jsonObject3.put("categoryName", item.getCategory3Name());
                    return jsonObject3;
                }).collect(Collectors.toList());
                jsonObject2.put("categoryChild", categoryViewList2);
            }
            jsonObject.put("categoryChild", chile2);
            list.add(jsonObject);
        }

        return list;
    }


    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        //调用mapper层方法
        return baseAttributeMapper.selectBaseAttributeList(category1Id);
    }

    @Override
    public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
        //根据三级分类id获三级分类数据
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
        return baseCategoryView;
    }
}
